* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/data/merged_for_reg, clear
	
	xtset ctycode year


* set reference bin

	rename dd5bin10 REF_BIN	

	
	tempfile r2 r3	
	
* product-level results (Figure 5)
	
	
	estimates clear
	foreach x in total_meat_production pork_mutton_beef_prod poultry_rabbit_meat_prod slaughter_pig ///
		 milk_production  eggs_production {
	*****north*****	
		qui reghdfe ln_`x' dd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==1,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store n_`x'
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")

	******south******	
		qui reghdfe ln_`x' dd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==2,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store s_`x'
		parmest , label saving(`"`r3'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")	

			preserve 
			use `"`r2'"',clear
			append using `"`r3'"' 
			gen product = "`x'"
			rename id  region
			save $temp/est_lvstkprd_`x',replace
			restore			
					
			}	
	
	*esttab s_*, b(4) se(4) keep(dd*bin*) brackets	
	
	
	clear
	foreach x in total_meat_production pork_mutton_beef_prod poultry_rabbit_meat_prod slaughter_pig ///
		 milk_production  eggs_production {
		append using $temp/est_lvstkprd_`x'
	}
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = 2.5 if strmatch(parm, "dd5bin0")
	replace level = 7.5 if strmatch(parm, "dd5bin5")
	replace level = -2.5 if strmatch(parm, "dd5binNeg")
	*replace level = 12.5 if strmatch(parm, "dd5bin10")
	replace level = 17.5 if strmatch(parm, "dd5bin15")
	replace level = 22.5 if strmatch(parm, "dd5bin20")
	replace level = 27.5 if strmatch(parm, "dd5bin25")
	replace level = 32.5 if strmatch(parm, "dd5bin30")
	replace level = 37.5 if strmatch(parm, "dd5bin35")

	
	// product name 
	replace product = "Total Meat" if strmatch(product,"total_meat_production")
	replace product = "Pork, Mutton, Beef" if strmatch(product,"pork_mutton_beef_prod")
	replace product = "Poultry Meat" if strmatch(product,"poultry_rabbit_meat_prod")
	replace product = "Pig Slaughter" if strmatch(product,"slaughter_pig")
	replace product = "Milk" if strmatch(product,"milk_production")
	replace product = "Egg" if strmatch(product,"eggs_production")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort region product : gen last = _n == _N
	expand 2 if last
	sort region product parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]		
	
	save $visual/visual_livestock,replace	
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}	
